/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller <philipp.mueller@gmx.de>         *
 *   Copyright (C) 2008 by Tanguy Krotoff <tkrotoff@gmail.com>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtGui>

#include <phonon/audiooutput.h>

#include "widgetvideo.h"
#include "qtvomp.h"
#include "vompglobal.h"

WidgetVideo::WidgetVideo( QWidget *parent ) : Phonon::VideoWidget( parent )
{
}


WidgetVideo::~WidgetVideo()
{
}


void WidgetVideo::mousePressEvent( QMouseEvent * event )
{
    qDebug() << "WidgetVideo::mouseClickEvent called";

    Phonon::VideoWidget::mousePressEvent( event );
    WidgetPlayVideo* parent = static_cast<WidgetPlayVideo*>( this->parent() );
    parent->osd()->toggleOsd();
}


void WidgetVideo::mouseDoubleClickEvent( QMouseEvent * event )
{
    Phonon::VideoWidget::mouseDoubleClickEvent( event );
    setFullScreen( !isFullScreen() );
}


void WidgetVideo::keyPressEvent( QKeyEvent * event )
{
    WidgetPlayVideo* parent = static_cast<WidgetPlayVideo*>( this->parent() );
    parent->handleKeyPressEvent( event );
    QWidget::keyPressEvent( event );
}


bool WidgetVideo::event( QEvent * event )
{
    WidgetPlayVideo* parent;

    switch ( event->type() )
    {

        case QEvent::Close:
            //We just ignore the close event on the video widget
            //this prevents ALT+F4 from having an effect in fullscreen mode
            event->ignore();
            return true;

        case QEvent::MouseMove:

            if ( isFullScreen() )
            {
                m_timer.start( 1000, this );
            }

            unsetCursor();

            break;

        case QEvent::WindowStateChange:

            if ( isFullScreen() )
            {
                m_timer.start( 1000, this );
            }
            else
            {
                m_timer.stop();
                unsetCursor();
            }
            parent = static_cast<WidgetPlayVideo*>( this->parent() );
            QTimer::singleShot( 1000, parent->osd(), SLOT( centerWidget() ) );

            break;

        default:
            break;
    }

    return Phonon::VideoWidget::event( event );
}


void WidgetVideo::timerEvent( QTimerEvent * event )
{
    qDebug() << "MyVideoWidget::timerEvent";

    if ( event->timerId() == m_timer.timerId() )
    {
        //Let's store the cursor shape
        setCursor( Qt::BlankCursor );
        m_timer.stop();

        qDebug() << "MyVideoWidget::timerEvent hide the cursor";
    }

    Phonon::VideoWidget::timerEvent( event );
}
